﻿/*----------------------------------------------------------------
//Copyright (C) 2012 赵国伟
//文件功能描述：快递费用管理客户端脚本
//创建人：赵国伟
//生成时间：2012-04-22 00:07:46
//----------------------------------------------------------------*/
var AjaxServerUrl = "/Manage/AS/BaseDataCommon.aspx";
var ModelOfDeliveryFee;
var DeliveryFeeID = 0;
var SelectOptionOfDelivery = "";
var SelectOptionOfProvince = "";
var tableOfAreaName = "tableOfArea";
var curRowOfArea = 0;
var curAreaID = 0;

///初始化
function Init() {
    DeliveryFeeID = 0;
    curRowOfArea = 0;
    curAreaID = 0;
}

function GoToSearch() {
    var url = "DeliveryFeesManage.aspx?";
    url += "DeliveryID=" + $("#ddlDeliveryOfSearch").val();
    url += "&ProvinceID=" + $("#ddlProvinceIDOfSearch").val();
    url += "&CityID=" + $("#ddlCityIDOfSearch").val();
    url += "&AreaID=" + $("#ddlAreaOfSearch").val();
    location.href = url;
}


//执行删除
function Delete(id) {
    if (!confirm("系统提示：您确定要删除该条快递费用信息？"))
        return;
    AjaxGet(AjaxServerUrl + "?Action=DeleteDeliveryFee&DeliveryFeeID=" + id);
}

///检查表单；
function CheckForm() {
    if ($("#ddlDelivery").val() == "") {
        alert("系统提示：请选择快递方式！");
        $("#ddlDelivery").focus();
        return false;
    }
    if ($("#txtFirstWeight").val() == "" || !isMoney($("#txtFirstWeight").val())) {
        alert("系统提示：首重不能为空或者必须为数字！");
        $("#txtFirstWeight").focus();
        return false;
    }
    if ($("#txtFirstWeightFee").val() == "" || !isMoney($("#txtFirstWeightFee").val())) {
        alert("系统提示：首重费用不能为空或者必须为数字！");
        $("#txtFirstWeightFee").focus();
        return false;
    }
    if ($("#txtPerWeight").val() == "" || !isMoney($("#txtPerWeight").val())) {
        alert("系统提示：续重不能为空或者必须为数字！");
        $("#txtPerWeight").focus();
        return false;
    }
    if ($("#txtPerWeightFee").val() == "" || !isMoney($("#txtPerWeightFee").val())) {
        alert("系统提示：续重费用不能为空或者必须为数字！");
        $("#txtPerWeightFee").focus();
        return false;
    }
    if (curAreaID <= 0) {
        if (curRowOfArea <= 0) {
            alert("系统提示：请设置快递费适用的区域！");
            return false;
        }
        for (var i = 0; i < curRowOfArea; ++i) {
            if ($("#ddlAreaOf" + i).val() == "") {
                alert("系统提示：请选择区域" + (i + 1) + "！");
                $("#ddlAreaOf" + i).focus();
                return false;
            }
        }
        //判断区域名称是否重复；
        var hashArea = new HashTable();
        for (var i = 0; i < curRowOfArea; ++i) {
            if (!hashArea.contains($("#ddlAreaOf" + i).val()))
                hashArea.add($("#ddlAreaOf" + i).val());
            else {
                alert("系统提示：区域" + (i + 1) + "重复！");
                $("#ddlAreaOf" + i).focus();
                return false;
            }
        }
    }
    return true;
}

//保存；
function Save() {
    $("#btnSave").attr("disabled", true);
    if (!CheckForm()) {
        $("#btnSave").attr("disabled", false);
        return;
    }
    var data = clone(ModelOfDeliveryFee);
    data = { "_areaid": 0, "_createdate": "\/Date(1335066730432+0800)\/", "_deliveryfeesid": 0, "_deliveryid": 0, "_firstweight": 0, "_firstweightfee": 0, "_perweight": 0, "_perweightfee": 0 };
    data._deliveryfeesid = DeliveryFeeID;
    data._areaid = curAreaID;
    data._deliveryid = $("#ddlDelivery").val();
    data._createdate = "/Date(" + CurrentDateStringToUTC() + ")/";
    data._firstweight = $("#txtFirstWeight").val();
    data._firstweightfee = $("#txtFirstWeightFee").val();
    data._perweight = $("#txtPerWeight").val();
    data._perweightfee = $("#txtPerWeightFee").val();

    var AreaIds = new Array();
    for (var i = 0; i < curRowOfArea; ++i)
        AreaIds.push($("#ddlAreaOf" + i).val());
    
    var url = AjaxServerUrl + "?Action=SaveDeliveryFee";
    var postData = "jsonData=" + JSON.stringify(eval(data));
    if (DeliveryFeeID <= 0)
        postData += "&JsonOfAreaIDs=" + JSON.stringify(eval(AreaIds));
   
    AjaxPostEx(url, "btnSave", postData);  //更新数据；
    $("#btnSave").attr("disabled", false);
}

//绑定快递费用信息到录入表单中；
function BindDeliveryFeeToForm() {
    var url = AjaxServerUrl + "?action=GetDeliveryFee&DeliveryFeeID=" + DeliveryFeeID + "&tttt=" + Math.random();
    $.ajax(
    {
        type: "Get",
        url: url,
        dataType: "json",
        success: function (data) {
            if (data != null) {
                $("#ddlDelivery").val(data.Fee._deliveryid);
                $("#txtFirstWeight").val(data.Fee._firstweight);
                $("#txtFirstWeightFee").val(data.Fee._firstweightfee);
                $("#txtPerWeight").val(data.Fee._perweight);
                $("#txtPerWeightFee").val(data.Fee._perweightfee);
                curAreaID = data.Fee._areaid;
                $("#trArea").show();
                $("#spAreaName").html(data.Province._name + " >> " + data.City._name + " >>" + data.AreaName);
            }
        }
    });
}

///添加界面；
function Add() {
    Init();
    GetPopupDiv(600, 800, "#e2e2e2", "添加快递费用", GetHtmlForm());
}

//编辑界面；
function Edit(id) {
    Init();
    DeliveryFeeID = id;
    GetPopupDiv(600, 800, "#e2e2e2", "编辑快递费用", GetHtmlForm());
    BindDeliveryFeeToForm();
}

///获取录入界面的HTML;
function GetHtmlForm() {
    var html = new Array();
    html.push("<form>");
    html.push("<table width=\"600\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table\">");
    html.push("  <tr>");
    html.push("    <td colspan=\"2\" class=\"TableHeadRow\">&gt;&gt;快递费用设置</td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td width=\"90\" align=\"right\">快递方式：</td>");
    html.push("    <td width=\"496\"><select name=\"ddlDelivery\" id=\"ddlDelivery\">");
    html.push("      <option value=\"\">--请选择快递方式--</option>");
    html.push(SelectOptionOfDelivery);
    html.push("    </select>");
    html.push("    <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr id=\"trArea\" style=\"display:none;\">");
    html.push("    <td align=\"right\" class=\"red bold\">适用区域：</td>");
    html.push("    <td><span id=\"spAreaName\" class=\"red bold\"></span></td>");
    html.push("  </tr>");

    html.push("  <tr>");
    html.push("    <td align=\"right\">首重：</td>");
    html.push("    <td><input name=\"txtFirstWeight\" type=\"text\"  class=\"border6\" id=\"txtFirstWeight\" size=\"7\" />g");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">首重费用￥：</td>");
    html.push("    <td><input name=\"txtFirstWeightFee\" type=\"text\"  class=\"border6\" id=\"txtFirstWeightFee\" size=\"7\" />");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">续重：</td>");
    html.push("    <td><input name=\"txtPerWeight\" type=\"text\"  class=\"border6\" id=\"txtPerWeight\" size=\"7\" />g");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">续重费用￥：</td>");
    html.push("    <td><input name=\"txtPerWeightFee\" type=\"text\"  class=\"border6\" id=\"txtPerWeightFee\" size=\"7\" />");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("</table>");

    if (DeliveryFeeID <= 0) {
        html.push("<table width=\"600\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table\" id=\"tableOfArea\">");
        html.push("  <tr>");
        html.push("    <td colspan=\"5\" class=\"TableHeadRow\">&gt;&gt; 应用区域</td>");
        html.push("  </tr>");
        html.push("  <tr>");
        html.push("    <td width=\"60\" align=\"center\">序号：</td>");
        html.push("    <td width=\"120\" align=\"left\">省份：</td>");
        html.push("    <td width=\"150\" align=\"left\">城市</td>");
        html.push("    <td align=\"left\">区域</td>");
        html.push("    <td width=\"50\" align=\"center\">[<a href=\"javascript:AddAreaRow();\" class=\"red\">添加</a>]</td>");
        html.push("  </tr>");
        html.push("  <tr id=\"trOfTips\">");
        html.push("    <td colspan=\"5\" align=\"center\" class=\"red\" >[ 您还没有添加任何区域 ]</td>");
        html.push("    </tr>");
        html.push("</table>");
    }

    html.push("<table width=\"600\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table\">");
    html.push("  <tr>");
    html.push("    <td align=\"center\"><input name=\"btnSave\" type=\"button\" id=\"btnSave\" value=\" 保 存 \" class=\"btn2\" onclick=\"Save();\" />");
    html.push("      <input name=\"btnReset\" type=\"reset\" id=\"btnReset\" value=\" 重 置 \" class=\"btn2\" />");
    html.push("      <input name=\"btnClose\" type=\"button\" id=\"btnClose\" value=\"关闭本窗口\" class=\"btn2\" onclick=\"yesOther(false);\" /></td>");
    html.push("  </tr>");
    html.push("</table>");
    html.push("</form>");

    return html.join("");
}


///添加区域行；
function AddAreaRow() {
    $("#trOfTips").hide();
    var tr = oo(tableOfAreaName).insertRow(oo(tableOfAreaName).rows.length-1);
    td = tr.insertCell(0);
    td.innerHTML = ((curRowOfArea + 1) + ".");  //序号列；
    td.align = "center";

    td = tr.insertCell(1);
    td.innerHTML = "<select name=\"ddlProvinceOf" + curRowOfArea + "\" id=\"ddlProvinceOf" + curRowOfArea + "\" onchange=\"QueryCities(this.options[this.selectedIndex].value,'ddlCityOf" + curRowOfArea + "')\"><option value=\"\">--请选择省份--</option>" + SelectOptionOfProvince + "</select>";
    td.align = "left";

    td = tr.insertCell(2);
    td.innerHTML = "<select name=\"ddlCityOf" + curRowOfArea + "\" id=\"ddlCityOf" + curRowOfArea + "\" onchange=\"QueryAreas(this.options[this.selectedIndex].value,'ddlAreaOf" + curRowOfArea + "')\"><option value=\"\">--请选择城市--</option></select>";
    td.align = "left";

    td = tr.insertCell(3);
    td.innerHTML = "<select name=\"ddlAreaOf" + curRowOfArea + "\" id=\"ddlAreaOf" + curRowOfArea + "\" ><option value=\"\">--请选择区域--</option></select>";
    td.align = "left";

    td = tr.insertCell(4);
    td.innerHTML = "[ <a href=\"javascript:void(0);\" onclick=\"javascript:DeleteAreaRow(this);\" >删除</a> ]";
    td.align = "center";
    curRowOfArea++;
}

//删除区域行；
function DeleteAreaRow(objRow) {
    if (!confirm("确定要删除该行吗?"))
        return false;
    var rowIndex = objRow.parentNode.parentNode.rowIndex;
    oo(tableOfAreaName).deleteRow(rowIndex);
    if (oo(tableOfAreaName).rows.length == 3) {
        curRowOfArea = 0;
        $("#trOfTips").show();
    }
}













